package client.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import messages.*;
import messages.MessageLogoutReply.LogoutStatus;

/**
 * This constructor get order list and initialize button
 * 
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 * 
 * @param MainMenuType type, GUI_MainScreen ms
 */


public abstract class GUI_AbstractMainMenu extends JPanel {

	protected MainMenuType type;
	protected GUI_ImagePanel background;
	protected JLabel greetMsg = null;
	protected JLabel mainMenu = null;
	public JButton logOut = null;
	public Client_GUI ms;

	
	public GUI_AbstractMainMenu (MainMenuType type, Client_GUI ms) {
		
		this.type=type;
		this.ms=ms;
		initialize();
		
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	protected void initialize() {
		
		//*** Initializing GUI elements
		
		background = new GUI_ImagePanel(new ImageIcon("images/background1.jpg").getImage());
		background.setSize(new Dimension(1000, 700));
		
		greetMsg = new JLabel();
		greetMsg.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 24));
		greetMsg.setBounds(new Rectangle(50, 102, 370, 40));
		greetMsg.setForeground(Color.BLUE);
		
	
		mainMenu = new JLabel();
		mainMenu.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 50));
		mainMenu.setBounds(new Rectangle(384, 29, 388, 54));
		mainMenu.setText("Main Menu");
		mainMenu.setForeground(Color.BLUE);
		
		this.setLayout(null);
		this.setSize(1000, 700);
		this.setVisible(true);
		this.add(getLogout(), null);
		this.add(mainMenu, null);
		this.add(greetMsg, null);
		this.add(background, null);	
	}
	

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	protected JButton getLogout() {
		if (logOut == null) {
			logOut = new JButton();
			logOut.setText("Logout");
			logOut.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 20));
			logOut.setBounds(new Rectangle(800, 20, 146, 34));
			logOut.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				int i = JOptionPane.showConfirmDialog(null, "Are you sure you want to logut?", "Logut", JOptionPane.YES_NO_OPTION);
				if (i == JOptionPane.YES_OPTION){
				MessageLogout msgLog=new MessageLogout(ms.lg.getCurrentUserID(), 1);
				try {
					//Sending the login message to server
					
					     ms.client.sendToServer(msgLog);			
					

				} catch (IOException e1) {

					e1.printStackTrace();
				} 
				
				//Getting reply to login from client
				MessageLogoutReply msglogrep = (MessageLogoutReply) ms.client.GetMessage();
				
				if(msglogrep.getLogoutStatus()==LogoutStatus.LOGGED_OUT)
				{
					ms.lg=new GUI_Login(ms);
					ms.setContentPane(ms.lg);
				}
				
				else if(msglogrep.getLogoutStatus()==LogoutStatus.LOGOUT_FAIL)
				{
					JOptionPane.showMessageDialog(null,msglogrep.GetErrorMessage(1));
				}
			}}
		});
	}
	return logOut;
}
public enum MainMenuType {
		
		GUEST, 
		STUDENT, 
		TEACHER, 
		MANAGER,
		ADMIN
		
	}

final public MainMenuType getMainMenuType() {
	
	return type;
	
}

	public void switchToMainManu()
	{
		switch(type)
		{
			case GUEST:
			
			ms.setContentPane(ms.gst);
			break;
			
			case STUDENT:
				
			ms.setContentPane(ms.stdnt);
			break;
			
			case TEACHER:
				
			ms.setContentPane(ms.tchr);
			break;
			
			case MANAGER:
			ms.setContentPane(ms.mngr);
			break;
			
			case ADMIN:
			ms.setContentPane(ms.admn);
			break;
	
		}
	}	
}
